package com.atnjupt.admin.controller;

import com.atnjupt.admin.bean.User;
import com.atnjupt.admin.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Wizarder
 * @create 2021-10-11 22:14
 */
@Controller
public class TablesController {
    @Autowired
    UserService userService;

    @GetMapping("/basic_table")
    public String basicTable() {
        return "tables/basic_table";
    }

    @GetMapping("/dynamic_table/delete/{id}")
    public String deleteUser(@PathVariable("id") Integer id,
                             @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                             RedirectAttributes redirectAttributes){
        userService.removeById(id);
        redirectAttributes.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }

    @GetMapping("/dynamic_table")
    public String dynamicTable(@RequestParam(value = "pn",defaultValue = "1") Integer pn,
                               HttpSession httpSession,
                               Model model) {
//        User user = (User) httpSession.getAttribute("user");
//        List<User> users = Arrays.asList(user, new User("李嘉欣", "lix123456"));
//        model.addAttribute("users",users);
        //构造分页参数
        Page<User> page = new Page<>(pn,2);
        //调用page进行分页
        Page<User> userPage = userService.page(page,null);
        List<User> records = userPage.getRecords();
        boolean prePage = userPage.hasPrevious();
        //获取当前页码
        long current = userPage.getCurrent();
        //条目数
        long total = userPage.getTotal();
        //总页码数
        long pages = userPage.getPages();

        System.out.println(records.toString());
        model.addAttribute("userpage",userPage);
        return "tables/dynamic_table";
    }

    @GetMapping("editable_table")
    public String editableTable() {
        return "tables/editable_table";
    }

    @GetMapping("/responsive_table")
    public String responsiveTable() {
        return "tables/responsive_table";
    }
}
